package com.xiaode.util.hikvision;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.xiaode.common.model.Camera;
import com.xiaode.common.model.dto.hikvision.HKCameraDTO;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * "cameraIndexCode": "09c40d36c39f4b90821fa6f48c526392",
 *
 */
public class HikvisionUtil {
	
	private static final String Host = "192.168.1.64:443";
	private static final String AppKey = "21078286";
	private static final String AppSecret = "3nmxi9dVZghhdzQVNJkI";
	
	private static final String callBackUrl = "http://192.168.1.123:1234/callbackHK/hikvisionReturn";
	private static final String https = "https://";
	private static final String contentType = "application/json";
	private static final String ARTEMIS_PATH = "/artemis";
	private static final String wss = "ws";
	
	private static final String eventSubscriptionByEventTypes = "/api/eventService/v1/eventSubscriptionByEventTypes";
	private static final String addition = "/api/frs/v1/face/single/addition";//单个添加人脸
	private static final String previewURLs = "/api/video/v2/cameras/previewURLs";//获取监控点预览取流
	private static final String playbackURLs = "/api/video/v2/cameras/playbackURLs";//获取监控点回放取流
	private static final String cameraSearch = "/api/resource/v2/camera/search";//查询监控点列表
	private static final String updateition = "/api/frs/v1/face/single/update";//单个修改人脸
//	private static final String getFaceGroup = "/api/frs/v1/face/group";//获取人脸分组
	
	/**
	 * 按事件类型订阅事件
	 * 人脸比对事件:1644175361
	 */
	public static JSONObject eventSubscriptionByEventTypes(int[] eventTypes) throws Exception {
		
		ArtemisConfig config = new ArtemisConfig();
		config.setHost(Host); // 代理API网关nginx服务器ip端口
		config.setAppKey(AppKey);  // 秘钥appkey
		config.setAppSecret(AppSecret);// 秘钥appSecret
		
		Map<String, Object> paramMap = new HashMap<String, Object>();// post请求Form表单参数
		paramMap.put("eventTypes", eventTypes);
		paramMap.put("eventDest", callBackUrl);
		paramMap.put("subType", 0);
		
		Map<String, String> header = new HashMap<String, String>();
		header.put("userId", "admin");
		
		Map<String, String> path = new HashMap<String, String>();
		path.put(https, ARTEMIS_PATH + eventSubscriptionByEventTypes);
		
		String rsp = ArtemisHttpUtil.doPostStringArtemis(config, path, JSONUtil.toJsonStr(paramMap), null, null, contentType, header);
		JSONObject jo = JSONUtil.parseObj(rsp);
		int retcode = jo.getInt("code");
		if(retcode != 0) {
			System.err.println(jo);
			return null;
		}
		return jo;
	}
	
	
	/**
	 * 添加人脸
	 */
	public static String addition(String name, String faceBinaryData, String faceGroupIndexCode) throws Exception {
		
		ArtemisConfig config = new ArtemisConfig();
		config.setHost(Host); // 代理API网关nginx服务器ip端口
		config.setAppKey(AppKey);  // 秘钥appkey
		config.setAppSecret(AppSecret);// 秘钥appSecret
		
		JSONObject faceInfo = JSONUtil.createObj();
		faceInfo.set("name", name);
		JSONObject facePic = JSONUtil.createObj();
		facePic.set("faceBinaryData", faceBinaryData);//人脸图片的二进制数据经过Base64编码后的字符串，和faceUrl不能同时为空，同时存在时优先取faceBinaryData。 图片的大小范围在10KB到200KB之间，只支持JGP格式图片。最大长度：200*1024
		
		JSONObject param = JSONUtil.createObj();
		param.set("faceGroupIndexCode", faceGroupIndexCode);
		param.set("faceInfo", faceInfo);
		param.set("facePic", facePic);
		
		Map<String, String> path = new HashMap<String, String>();
		path.put(https, ARTEMIS_PATH + addition);
		
		String rsp = ArtemisHttpUtil.doPostStringArtemis(config, path, param.toString(), null, null, contentType);
		JSONObject jo = JSONUtil.parseObj(rsp);
		int retcode = jo.getInt("code");
		if(retcode != 0) {
			System.err.println(jo);
			return null;
		}
		String indexCode = jo.getByPath("data.indexCode", String.class);//人脸的唯一标识
		
		return indexCode;
	}
	
	/**
	 * 修改人脸
	 * @throws Exception 
	 */
	public static Boolean updateition(String faceCode, String faceBinaryData) throws Exception{
		ArtemisConfig config = new ArtemisConfig();
		config.setHost(Host); // 代理API网关nginx服务器ip端口
		config.setAppKey(AppKey);  // 秘钥appkey
		config.setAppSecret(AppSecret);// 秘钥appSecret
		
		JSONObject facePic = JSONUtil.createObj();
		facePic.set("faceBinaryData", faceBinaryData);
		JSONObject param = JSONUtil.createObj();
		param.set("indexCode", faceCode);
		param.set("facePic", facePic);
		
		Map<String, String> path = new HashMap<String, String>();
		path.put(https, ARTEMIS_PATH + updateition);
		
		String rsp = ArtemisHttpUtil.doPostStringArtemis(config, path, param.toString(), null, null, contentType);
		JSONObject jo = JSONUtil.parseObj(rsp);
		int retcode = jo.getInt("code");
		if(retcode != 0) {
			System.err.println(jo);
			return false;
		}
		
		return jo.getByPath("data", Boolean.class);
	}
	
	/**
	 * 获取wss流链接
	 */
	public static String getPreviewURLs(String cameraIndexCode) throws Exception {
		
		ArtemisConfig config = new ArtemisConfig();
		config.setHost(Host); // 代理API网关nginx服务器ip端口
		config.setAppKey(AppKey);  // 秘钥appkey
		config.setAppSecret(AppSecret);// 秘钥appSecret
		
		JSONObject param = JSONUtil.createObj();
		param.set("cameraIndexCode", cameraIndexCode);
		param.set("protocol", wss);
		
		Map<String, String> path = new HashMap<String, String>();
		path.put(https, ARTEMIS_PATH + previewURLs);
		
		String rsp = ArtemisHttpUtil.doPostStringArtemis(config, path, param.toString(), null, null, contentType);
		JSONObject jo = JSONUtil.parseObj(rsp);
		int retcode = jo.getInt("code");
		if(retcode != 0) {
			System.err.println(jo);
			return null;
		}
		String wssUrl = jo.getByPath("data.url", String.class);
		
		return wssUrl;
	}
	
	/**
	 * 获取回放wss流
	 * UTC_MS_FORMAT
	 */
	public static String getPlaybackURLs(String cameraIndexCode, String beginTime, String endTime) throws Exception {
		
		ArtemisConfig config = new ArtemisConfig();
		config.setHost(Host); // 代理API网关nginx服务器ip端口
		config.setAppKey(AppKey);  // 秘钥appkey
		config.setAppSecret(AppSecret);// 秘钥appSecret
		
		JSONObject param = JSONUtil.createObj();
		param.set("cameraIndexCode", cameraIndexCode);
		param.set("recordLocation", 1);//存储类型,0：中心存储,1：设备存储,默认为中心存储,目前只能1
		param.set("protocol", wss);
		param.set("beginTime", beginTime);
		param.set("endTime", endTime);
		
		Map<String, String> path = new HashMap<String, String>();
		path.put(https, ARTEMIS_PATH + playbackURLs);
		String rsp = ArtemisHttpUtil.doPostStringArtemis(config, path, param.toString(), null, null, contentType);
		JSONObject jo = JSONUtil.parseObj(rsp);
		int retcode = jo.getInt("code");
		if(retcode != 0) {
			System.err.println(jo);
			return null;
		}
		String wssUrl = jo.getByPath("data.url", String.class);
		
		return wssUrl;
	}
	
	/**
	 * 保存和更新监控设备
	 */
	public static List<HKCameraDTO> getAllCamera(Integer pageNo, Integer pageSize) throws Exception {

		ArtemisConfig config = new ArtemisConfig();
		config.setHost(Host); // 代理API网关nginx服务器ip端口
		config.setAppKey(AppKey);  // 秘钥appkey
		config.setAppSecret(AppSecret);// 秘钥appSecret
		
		JSONObject param = JSONUtil.createObj();
		param.set("pageNo", pageNo);
		param.set("pageSize", pageSize);
		
		Map<String, String> path = new HashMap<String, String>();
		path.put(https, ARTEMIS_PATH + cameraSearch);
		
		String rsp = ArtemisHttpUtil.doPostStringArtemis(config, path, param.toString(), null, null, contentType);
		JSONObject jo = JSONUtil.parseObj(rsp);
		int retcode = jo.getInt("code");
		if(retcode != 0) {
			System.err.println(jo);
			return null;
		}
		JSONObject data = jo.getJSONObject("data");
		JSONArray list = data.getJSONArray("list");
		List<HKCameraDTO> HKCameraList = JSONUtil.toList(list, HKCameraDTO.class);
		for(HKCameraDTO DTO : HKCameraList) {
			Camera camera = Camera.dao.findFirst("SELECT * FROM camera WHERE camera_code = ?", DTO.getIndexCode());
			if(BeanUtil.isEmpty(camera)) {
				camera = new Camera();
				camera.setCameraCode(DTO.getIndexCode());
				camera.setCameraName(DTO.getName());
				camera.setRegionCode(DTO.getRegionIndexCode());
				camera.setRegionName(DTO.getRegionName());
				camera.save();
			}else {
				camera.setCameraCode(DTO.getIndexCode());
				camera.setCameraName(DTO.getName());
				camera.setRegionCode(DTO.getRegionIndexCode());
				camera.setRegionName(DTO.getRegionName());
				camera.update();
			}
		}
		
		return HKCameraList;
	}

	
}
